package com.xinzhitong.www.mapper;

import com.xinzhitong.www.pojo.Company;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * company表相关操作
 *
 * @author 徐鑫
 */
@Repository
public interface CompanyMapper {

    @Select("select * from company_view where id=#{id}")
    Company selectCompanyById(@Param("id") Integer id);

    @Select("select picture from company_has_picture where company_id=#{id}")
    String[] selectPictureByCompanyId(@Param("id") Integer id);

    @Insert("insert into user_comment_company(username, company_id, text) values(#{username}, #{companyId}, #{text})")
    Integer insertUserCommentCompany(
            @Param("username") String username,
            @Param("companyId") Integer companyId,
            @Param("text") String text
    );

    @Select("select * from user_comment_company_view where companyId=#{companyId}")
    List<Map<String, Object>> selectUserCommentCompanyByCompanyId(@Param("companyId") Integer companyId);

    @Insert("insert into company(" +
            "name,\n" +
            "star,\n" +
            "introduction,\n" +
            "industry,\n" +
            "scale,\n" +
            "date,\n" +
            "province,\n" +
            "city,\n" +
            "area,\n" +
            "address,\n" +
            "contact,\n" +
            "shortname,\n" +
            "nature,\n" +
            "logo,\n" +
            "username\n" +
            ") values(" +
            "#{name},\n" +
            "#{star},\n" +
            "#{introduction},\n" +
            "#{industry},\n" +
            "#{scale},\n" +
            "#{date},\n" +
            "#{province},\n" +
            "#{city},\n" +
            "#{area},\n" +
            "#{address},\n" +
            "#{contact},\n" +
            "#{shortname},\n" +
            "#{nature},\n" +
            "#{logo},\n" +
            "#{username}\n" +
            ")")
    Integer insertCompany(Company company);

    @Select("select * from company where username=#{username}")
    Company selectCompanyByUsername(@Param("username") String username);

    @Update("update company set ${field}=#{value} where ${condition} ")
    Integer updateCompanyOfOneFieldByCondition(@Param("field") String field, @Param("value") Object value, @Param("condition") String condition);

    @Insert("insert into company_has_picture values(#{companyId}, #{picture})")
    Integer insertCompanyPicture(@Param("companyId") Integer companyId, @Param("picture") String picture);

    @Delete("delete from company_has_picture where company_id=#{companyId}")
    void deleteCompanyPicture(@Param("companyId") Integer companyId);

    @Select("SELECT column_type " +
            "FROM information_schema.COLUMNS " +
            "WHERE TABLE_SCHEMA=\"xinzhitong\" " +
            "AND DATA_TYPE='enum' " +
            "AND table_name=\"company\" " +
            "AND column_name=\"${field}\"")
    String selectEnumOf(@Param("field") String field);

}
